package com.ljh.web;

import com.ljh.bean.Menu;
import com.ljh.bean.Role;
import com.ljh.bean.Users;
import com.ljh.service.MenuService;
import com.ljh.service.RoleService;
import com.ljh.service.impl.MenuServiceImpl;
import com.ljh.service.impl.RoleServiceImpl;
import com.ljh.util.PageUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns="/power/menu/menus")
public class MenuServlet  extends HttpServlet {
    private RoleService roleService=new RoleServiceImpl();
    private MenuService menuService=new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if("insert".equals(method)){
            insert(req,resp);
        } else if ("getDetails".equals(method)){
            getDetails(req, resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if("findbyid".equals(method)){
            findbyid(req, resp);
        }else if("delete".equals(method)){
            delete(req, resp);
        }else if("selectmenus".equals(method)){
            selectmenus(req,resp);
        }
    }
    //查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        //2.调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    /**
     * 删除菜单管理
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String menuid = req.getParameter("menuid");
        //调取service方法
        int i = menuService.deletebyid(menuid);//成功返回1，失败返回0
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<script>alert('删除成功');location.href='/power/menu/menus?method=select'</script>");
    }
    //主键查询
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String menuid = req.getParameter("menuid");
        //根据主键查询用户信息
        Menu menu = menuService.findById(Integer.parseInt(menuid));
        List menuList = menuService.getList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("menu",menu);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    /**
     * 修改菜单管理信息
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String menuid = req.getParameter("menuid");
        String menuname = req.getParameter("menuname");
        String state = req.getParameter("state");
        String[] menuids = {"1","2"};
        //调取service方法
        int i = menuService.updateMenu(menuname,state,menuids,menuid);//成功count

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        //修改成功
        if(i == menuids.length){
            writer.println("<script>alert('修改成功');location.href='/power/menu/menus?method=select'</script>");
        }else{//修改失败
            writer.println("<script>alert('修改失败,请检查输入信息');location.href='/power/menu/menus?method=findbyid&menuid="+Integer.parseInt(menuid)+"'</script>");
        }
    }
    //详情页面表示
    protected void getDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuid = req.getParameter("menuid");
        Menu menu = menuService.getDetails(Integer.parseInt(menuid));
        resp.setContentType("text/html;charset=utf-8");
        req.setAttribute("menulist",menu);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
    //新增角色
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuname = req.getParameter("menuname");
        String upmenuid = req.getParameter("upmenuid");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String desc = req.getParameter("desc");
        int i = menuService.insertMenu(menuname, upmenuid, state,url,desc);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('添加成功');location.href='/power/menu/menus?method=select'</script>");
        }else{
            writer.println("<script>alert('添加失败');location.href='/power/menu/menus?method=select'</script>");
        }
    }
    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Menu> menuList = menuService.getMenuList(pageIndex, pageUtil.getPageSize());
        int total = menuService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(menuList);
        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}